package com.hy.mydesktop.client.component.factory;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.hy.mydesktop.client.component.meta.AccordionPanelModelEnum;
import com.hy.mydesktop.client.component.meta.GridSetModulesEnum;
import com.hy.mydesktop.client.component.meta.WindowModelEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.client.mvc.core.event.manager.ComponentTrrigerTemplateUtil;
import com.hy.mydesktop.client.mvc.grid.GridSetModule;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.MetaDataConstants;
import com.hy.mydesktop.shared.rpc.util.ComponentTypeEnumConverter;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-21；时间：下午03:05:22</li>
 * <li>类型名称：FormModuleFactory</li>
 * <li>设计目的：创建表单组件</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class GridSetModulesFactory {

	public static ContentPanel createGridModule(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		return configureFormPanel(gxtComponentMetaNodeModel);
/**
 * 
 */
	}

	
	private static ContentPanel configureFormPanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		GridSetModule gridSetModule = createDefaultGridSetModule();
//		gridSetModule.setAutoWidth(true);
//		AbstractComponetFactory.configureBaseComponentId(gridSetModule, gxtComponentMetaNodeModel);
		AbstractComponetFactory.configureAbstractComponet(gridSetModule, gxtComponentMetaNodeModel);
		gridSetModule = (GridSetModule)AbstractLayoutContainerComponetFactory.configureAbstractLayoutContainerComponet(gridSetModule, gxtComponentMetaNodeModel);
		gridSetModule= (GridSetModule)ContentPanelFactory.configureAttributesOfContentPanel(gridSetModule, gxtComponentMetaNodeModel);

		 
		 /**
		map2.put("gridlist", list);
		  */
		
		
		if (gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.GRIDMODULE_TYPE) != null) {
					 		 
				gridSetModule.add(createBaseGxtComponent2(gxtComponentMetaNodeModel));		 
	    }else {
			
	    	if (gxtComponentMetaNodeModel.get(GridSetModulesEnum.GRIDLIST.name()
					.toLowerCase()) != null) {
				 List<GxtComponentMetaNodeModel> list = (List<GxtComponentMetaNodeModel>) gxtComponentMetaNodeModel.get(GridSetModulesEnum.GRIDLIST.name().toLowerCase());
				 //遍历GxtComponentMetaNodeModel的子节点，即GRIDLIST中的GxtComponentMetaNodeModel
				 for (GxtComponentMetaNodeModel subGxtComponentMetaNodeModel : list) {
					 //createBaseGxtComponent 
					 //表格与树形表格进行分支判断
						gridSetModule.add(createBaseGxtComponent(subGxtComponentMetaNodeModel));
				 }

	    	}
	    	
		}
		
		//按钮的对齐方式
		if (gxtComponentMetaNodeModel.get(GridSetModulesEnum.BUTTONALIGN
				.name().toLowerCase()) != null) {
			String buttonAlin = ((String) gxtComponentMetaNodeModel
					.get(GridSetModulesEnum.BUTTONALIGN.name().toLowerCase()))
					.toUpperCase();
			if (buttonAlin.equals("CENTER")) {
				gridSetModule.setButtonAlign(HorizontalAlignment.CENTER);
			}else if (buttonAlin.equals("LEFT")) {
				gridSetModule.setButtonAlign(HorizontalAlignment.LEFT);
			}
		}
		//折叠
		if (gxtComponentMetaNodeModel.get(GridSetModulesEnum.COLLAPSIBLE
				.name().toLowerCase()) != null) {
			Boolean collapsible = (Boolean) gxtComponentMetaNodeModel
					.get(GridSetModulesEnum.COLLAPSIBLE.name().toLowerCase());
			gridSetModule.setCollapsible((boolean) collapsible);

		} 
		//设置背景boolean
		
		//gridSetModule.setFrame((boolean) frame);

		//标题是否可见
		
		//标题
		if (gxtComponentMetaNodeModel.get(GridSetModulesEnum.HEADING.name()
				.toLowerCase()) != null) {
			String text = (String) gxtComponentMetaNodeModel
					.get(GridSetModulesEnum.HEADING.name().toLowerCase());
			gridSetModule.setHeading(text);

		} 
		
		if (gxtComponentMetaNodeModel.get(GridSetModulesEnum.ICON.name()
				.toLowerCase()) != null) {
			AbstractImagePrototype icon = (AbstractImagePrototype) gxtComponentMetaNodeModel
					.get(GridSetModulesEnum.ICON.name().toLowerCase());
			gridSetModule.setIcon(icon);

		}
		//自动调整宽度
		
		//自动调整高度
		
		//添加按钮
		if(gxtComponentMetaNodeModel.get(GridSetModulesEnum.ADDBUTTON.name()
				.toLowerCase()) != null){
			List<GxtComponentMetaNodeModel> list=(List<GxtComponentMetaNodeModel>)gxtComponentMetaNodeModel.get(GridSetModulesEnum.ADDBUTTON.name()
					.toLowerCase());
			for(int i=0;i<list.size();i++)
			{
				Button button=ButtonFactory.createButton(list.get(i));
		
				gridSetModule.addButton(button);
			}			
		}
		return gridSetModule;
	}

	private static GridSetModule createDefaultGridSetModule() {

		GridSetModule contentPanel = new GridSetModule();
		contentPanel.setSize(199, 100);
		contentPanel.setHeight(100);
		contentPanel.setLayout(new FormLayout());
		return contentPanel;
	}
	
	@SuppressWarnings("unchecked")
	private static <COMPONENT extends Component> COMPONENT createBaseGxtComponent(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		//BaseModel gxtComponentMetaModel=null;
		
		COMPONENT component = null;
		ComponentTypeEnum componentTypeEnum = null;		
		if (gxtComponentMetaNodeModel.getComponentTypeEnumIndex()!=0) {
			componentTypeEnum = 	ComponentTypeEnumConverter.getComponentTypeEnum(gxtComponentMetaNodeModel.getComponentTypeEnumIndex());

			
		}else {
			Object object = gxtComponentMetaNodeModel.get("component_model_type");
			
			if (object instanceof Integer) {
				Integer componentTypeEnumIndex = (Integer)gxtComponentMetaNodeModel.get(WindowModelEnum.LAYOUT.name().toLowerCase());	
				componentTypeEnum = 	ComponentTypeEnumConverter.getComponentTypeEnum(componentTypeEnumIndex);
			}else {
				componentTypeEnum = (ComponentTypeEnum)object;
			}
		}
		
		
		switch (componentTypeEnum) {
			//判别具体选择哪种表格，普通表格还是树形表格
		
			case TREEGRIDMODULE_MODEL: {
				AbstractComponetFactory.removeBaseGxtComponentId(gxtComponentMetaNodeModel);
	//			component = (COMPONENT) TreeGridModuleFactory.TreeGridModule(gxtComponentMetaNodeModel);
				break;
			}
			case GRIDMODULE_MODEL: {
				AbstractComponetFactory.removeBaseGxtComponentId(gxtComponentMetaNodeModel);
				component = (COMPONENT) GridModuleFactory.createAndConfigureGridModule(gxtComponentMetaNodeModel);
				break;
			}
			
			default: {//当componentTypeEnum为空，或者与现有控件类型不符合时
				component = null;
				break;
			}
			}
			return component;
	}
	
	
	private static <COMPONENT extends Component> COMPONENT createBaseGxtComponent2(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		//BaseModel gxtComponentMetaModel=null;
		
		COMPONENT component = null;
		ComponentTypeEnum GRIDMODULE_MODEL = ComponentTypeEnum.GRIDMODULE_MODEL;
		ComponentTypeEnum TREEGRIDMODULE_MODEL = ComponentTypeEnum.TREEGRIDMODULE_MODEL;
		
		String gridModuleType =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.GRIDMODULE_TYPE);
		if (GRIDMODULE_MODEL.toString().equals(gridModuleType)) {
			AbstractComponetFactory.removeBaseGxtComponentId(gxtComponentMetaNodeModel);
			component = (COMPONENT) GridModuleFactory.createAndConfigureGridModule(gxtComponentMetaNodeModel);
			
		}else if (TREEGRIDMODULE_MODEL.toString().equals(gridModuleType)) {
			AbstractComponetFactory.removeBaseGxtComponentId(gxtComponentMetaNodeModel);
			component = (COMPONENT) TreeGridModuleFactory.TreeGridModule(gxtComponentMetaNodeModel);
		} else {
			MyLoggerUtil.printError(GridSetModulesFactory.class, ComponentMetaDataConstants.GRIDMODULE_TYPE+"的类型应该是"+GRIDMODULE_MODEL.toString()
					+"或者是"+TREEGRIDMODULE_MODEL+",但，实际得到的是"+gridModuleType);
			
		}
		
			return component;
	}
	
	
	
}